//
//  SIXScaneViewController.m
//  weibo
//
//  Created by   李小云 on 16/5/22.
//  Copyright © 2016年 chuhan. All rights reserved.
//

#import "SIXScaneViewController.h"
#import "SIXMyCardController.h"

@interface SIXScaneViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
/**
 *  Y值约束
 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *XlayoutConstraint;
/**
 *  定时器
 */
@property(nonatomic,strong)CADisplayLink *playLink;
@end

@implementation SIXScaneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //设置导航栏内容
    // 设置tabBarItem的属性：
 
    self.navigationController.tabBarController.tabBar.tintColor = [UIColor whiteColor];
    self.navigationItem.title = @"扫一扫";
   
    [self setBarButtonItem:@"关闭" AndWhichButton:@"left" andNormalColor:[UIColor orangeColor]andHiglthColoc:[UIColor grayColor ]];
    
    [self setBarButtonItem:@"相册" AndWhichButton:@"right" andNormalColor:[UIColor orangeColor]andHiglthColoc:[UIColor grayColor ]];
   //定义定时器
    CADisplayLink *playlink = [CADisplayLink displayLinkWithTarget:self selector:@selector(scanning)];
    
    [playlink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
    _playLink = playlink;
}
/**
 *  方便定义导航栏左右按钮方法
 *
 *  @param str         <#str description#>
 *  @param dirction    <#dirction description#>
 *  @param NormalColor <#NormalColor description#>
 *  @param hightColor  <#hightColor description#>
 */
-(void )setBarButtonItem:(NSString *)str AndWhichButton:(NSString *)dirction andNormalColor:(UIColor *)NormalColor andHiglthColoc:(UIColor *)hightColor{
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:str forState:UIControlStateNormal];
    [btn setTitle:str forState:UIControlStateHighlighted];
    [btn setTitleColor: NormalColor forState:UIControlStateNormal];
    [btn setTitleColor: hightColor forState:UIControlStateHighlighted];
    [btn sizeToFit];
    if ([dirction isEqualToString:@"left"]) {
        
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:btn];
        [btn addTarget:self action:@selector(leftBtnClick) forControlEvents:UIControlEventTouchUpInside];
        
    }
    if ([dirction isEqualToString:@"right"]) {
        
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:btn];
         [btn addTarget:self action:@selector(rightBtnClick) forControlEvents:UIControlEventTouchUpInside];
        
    }
}
/**
 *  动态修改约束,实现扫描效果
 */
-(void)scanning{
    
    self.XlayoutConstraint.constant += 5;
    if (self.XlayoutConstraint.constant >= 130) {
        self.XlayoutConstraint.constant =-130;
    }
    
}
/**
 *  左按钮点击事件
 *
 *  @param btn <#btn description#>
 */
-(void)leftBtnClick{
    
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
/**
 *  右按钮点击事件
 *
 *  @param btn <#btn description#>
 */
-(void)rightBtnClick{
    
    //进入系统的相册 创建照片选择器
    
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
        return;
    
    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    //设置代理
    picker.delegate = self;
    //用户是否可以编辑相片
    picker.allowsEditing = YES;
    //设置数据源 系统相册
    //    UIImagePickerControllerSourceTypePhotoLibrary,
    //    UIImagePickerControllerSourceTypeCamera, 相机
    //    UIImagePickerControllerSourceTypeSavedPhotosAlbum
    picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    
    //弹出相册
    
    [self presentViewController:picker animated:YES completion:nil];
    
    
}

//用户选择时会调用
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    UIImage *image = info[UIImagePickerControllerOriginalImage];
    [self dismissViewControllerAnimated:YES completion:nil];

}
- (IBAction)MyCard:(UIButton *)sender {
    
    SIXMyCardController *Mycard= [[SIXMyCardController alloc]init];
    
    [self.navigationController pushViewController:Mycard animated:YES];
}
@end
